var read = false; // 判斷是否從資料庫取資料

var sex = "male";
var age = "NaN";
var weightType = "old";
var weight = "NaN";
var weight1 = "NaN";
var weight2 = "NaN";
var weight3 = "NaN";
var height = "NaN";
var bmi = "NaN";
var activity = "NaN";
var purview = "professional";
var calofday = "NaN";

var proportion_breakfast;
var proportion_lunch;
var proportion_dinner;

var milk = 0;
var low_milk = 0;// 低脂奶類
var high_milk = 0; // 高脂奶類
var veg = 0; // 蔬菜類
var fruit = 0;
var grain = 0;
var low_meat = 0;
var meat = 0;
var high_meat = 0;
var _fat = 0;

var quantity_Milk = 0;
var quantity_Veg = 0;
var quantity_Fruit = 0;
var quantity_Grain = 0;
var quantity_Meat = 0;
var quantity_Fat = 0;

var prot_g = 0;
var fat_g = 0;
var cho_g = 0;

var pre_prot = 0;
var pre_fat = 0;
var pre_cho = 0;

var nour_operand = "";
var nour_prot_g = 0, nour_fat_g = 0, nour_cho_g = 0, nour_cal = 0;

function radio_click(type) {
	var obj = document.getElementsByName(type);
	calofday = "NaN";

	if (type == "sex")
	{
		if (obj[0].checked == true)
			sex = obj[0].value;
		else
			sex = obj[1].value;
	} 
	else if (type == "weightType") 
	{
		if (obj[0].checked == true)
			weightType = obj[0].value;
		else
			weightType = obj[1].value;
	} 
	else if (type == "work") 
	{
		if (obj[0].checked == true)
			activity = obj[0].value;
		else if (obj[1].checked == true)
			activity = obj[1].value;
		else
			activity = obj[2].value;

		purview = "general";
		var obj2 = document.getElementsByName("activity");
		for ( var i = 0; i < obj2.length; i++)
			obj2[i].checked = false;
	} 
	else if (type == "activity") 
	{
		if (obj[0].checked == true)
			activity = obj[0].value;
		else if (obj[1].checked == true)
			activity = obj[1].value;
		else
			activity = obj[2].value;

		purview = "professional";
		var obj2 = document.getElementsByName("work");
		for ( var i = 0; i < obj2.length; i++)
			obj2[i].checked = false;
	}

	cal_calofday();
}

function get_sqldata(type1, type2, type3, type4) { // 從資料庫取資料
	read = true;
	sex = type1;

	weightType = type2;
	if (typeof type3 === 'undefined') {
		purview = "professional";
		activity = type4;
	} 
	else if (typeof type4 === 'undefined') {
		purview = "general";
		activity = type3;
	}

	age = parseFloat(document.getElementById('age').value);
	height = parseFloat(document.getElementById('height').value);
	weight1 = parseFloat(document.getElementById('weight').value);
	BMI();
}

function GetAge() {
	age = parseFloat(document.getElementById('age').value);
	if (age < 18)
		alert("本系統適合18歲以上。");
	cal_calofday();
}

function GetHeight() {
	height = parseFloat(document.getElementById('height').value);
	if (height < 100)
		alert("請確認身高。");
	BMI();
}

function GetWeight() {
	weight1 = parseFloat(document.getElementById('weight').value);
	document.getElementById('weight2').innerHTML = "";
	document.getElementById('weight3').innerHTML = "";
	if (weight1 < 20)
		alert("請確認體重。");
	BMI();
}

function BMI() {
	if (typeof weight1 === 'number' && typeof height === 'number') {
		bmi = (parseFloat(weight1 / (height * height / 10000))).toFixed(1);
		document.getElementById('bmi').innerHTML = "BMI: " + bmi;

		// 標準體重＝ 身高(公尺) * 身高(公尺) *22
		weight2 = (parseFloat((height * height / 10000) * 22)).toFixed(1);

		document.getElementById('weight2').innerHTML 
		= "標準體重: " + weight2 + "kg(" + (weight1 / weight2 * 100).toFixed(1) + "%)";

		// 調整體重 = 標準體重+0.25*(現在體重-標準體重)
		weight3 = (parseFloat(0.25 * weight1 + 0.75 * weight2)).toFixed(1);
		document.getElementById('weight3').innerHTML = weight3 + "kg";
		cal_calofday();
	} 
	else
		document.getElementById('bmi').innerHTML = "";
}

function cal_calofday() {
	if (weightType == "old")
		weight = weight1;
	else if (weightType == "new")
		weight = weight3;
	
	if (purview == "professional")
		ProCal();
	else if (purview == "general") 
		UserCal();
	
	document.getElementById("calofday").innerHTML = Math.round(calofday);
	document.getElementById("average2").innerHTML 
	= "(" + (calofday/weight1).toFixed(1) + "大卡/公斤)";

	if (read == false) {
		if (calofday >= 2000) // 2000大卡以上奶類2份
			quantity_Milk = 2;
		else if (calofday < 2000)
			quantity_Milk = 1.5;

		if (sex == "male") // 男生蔬菜5份、水果4份
		{
			quantity_Veg = 5;
			quantity_Fruit = 4;
		} else if (sex == "female") // female 女生蔬菜4份、水果3份
		{
			quantity_Veg = 4;
			quantity_Fruit = 3;
		}

		document.getElementById("quantity_Milk").value = quantity_Milk;
		document.getElementById("quantity_Veg").value = quantity_Veg;
		document.getElementById("quantity_Fruit").value = quantity_Fruit;	
	}
	ThreeNutrients();
}

function ProCal() {
	var bee;
	var pressure = parseFloat(document.getElementById('pressure').value);

	if (sex == "male")
		bee = 66 + 13.7 * weight + 5 * height - 6.8 * age;
	else
		// female 女性 BEE = 655+9.6x體重+1.8x身高- 4.7 x年齡
		bee = 655 + 9.6 * weight + 1.8 * height - 4.7 * age;
	//所需能量= BEE x  活動因子 x 壓力因子 - 營養品熱量
	if (activity == "little")
		calofday = bee * 1.2 * pressure;
	else if (activity == "middle")
		calofday = bee * 1.3 * pressure;
	else if (activity == "large")
		calofday = bee * 1.4 * pressure;

}

function UserCal() {
	document.getElementById('work1').innerHTML = "";
	document.getElementById('work2').innerHTML = "";
	document.getElementById('work3').innerHTML = "";
	//熱量 ＝ 活動因子 * 體重 - 營養品熱量
	if (bmi < 18.5) // 過輕
	{
		if (activity == "little") {
			calofday = 35 * weight;
			document.getElementById('work1').innerHTML = "(體重×35)";
		} 
		else if (activity == "middle") {
			calofday = 40 * weight;
			document.getElementById('work2').innerHTML = "(體重×40)";
		} 
		else if (activity == "large") {
			calofday = 45 * weight;
			document.getElementById('work3').innerHTML = "(體重×45)";
		}
	} // end if
	else if (bmi >= 18.5 && bmi < 24) // 正常
	{
		if (activity == "little") {
			calofday = 30 * weight;
			document.getElementById('work1').innerHTML = "(體重×30)";
		} 
		else if (activity == "middle") {
			calofday = 35 * weight;
			document.getElementById('work2').innerHTML = "(體重×35)";
		} 
		else if (activity == "large") {
			calofday = 40 * weight;
			document.getElementById('work3').innerHTML = "(體重×40)";
		}
	} // end else if
	else if (bmi >= 24 && bmi < 27) // 過重
	{
		if (activity == "little") {
			calofday = 25 * weight;
			document.getElementById('work1').innerHTML = "(體重×25)";
		} 
		else if (activity == "middle") {
			calofday = 30 * weight;
			document.getElementById('work2').innerHTML = "(體重×30)";
		} 
		else if (activity == "large") {
			calofday = 35 * weight;
			document.getElementById('work3').innerHTML = "(體重×35)";
		}
	} // end else if
	else if (bmi >= 27) // 肥旁(病態肥胖) BMI > 35
	{
		if (activity == "little") {
			calofday = 20 * weight;
			document.getElementById('work1').innerHTML = "(體重×20)";
		} 
		else if (activity == "middle") {
			calofday = 30 * weight;
			document.getElementById('work2').innerHTML = "(體重×30)";
		} 
		else if (activity == "large") {
			calofday = 35 * weight;
			document.getElementById('work3').innerHTML = "(體重×35)";
		}
	} // end else if
}

function Save_nourish_val(operand, prot_g, fat_g, cho_g) { //存取營養品三大營養素克數
	if (operand == "add") {
		nour_prot_g += prot_g;
		nour_fat_g += fat_g;
		nour_cho_g += cho_g;
		nour_cal = nour_prot_g * 4 + nour_fat_g * 9 + nour_cho_g * 4;
	} else if (operand == "del") {
		nour_prot_g -= prot_g;
		nour_fat_g -= fat_g;
		nour_cho_g -= cho_g;
		nour_cal = nour_prot_g * 4 + nour_fat_g * 9 + nour_cho_g * 4;
	}		
	SixType();
}

function ThreeNutrients() {
	var g_kgBW = parseFloat(document.getElementById('g_kgBW').value);
	var proportion_fat = parseFloat(document.getElementById('proportion_fat').value)// 脂肪百分比

	var prot = Math.round(weight*g_kgBW);	//克
	var prot__ = prot * 4;				//大卡
	var fat__ = Math.round(calofday*proportion_fat/100); //大卡
	var cho__ = Math.round(calofday - prot__ - fat__); //大卡

	document.getElementById('prot').innerHTML = prot;
	document.getElementById('prot__').innerHTML = prot__;
	document.getElementById('fat').innerHTML = (fat__/9).toFixed(0);
	document.getElementById('fat__').innerHTML = fat__;
	document.getElementById('cho').innerHTML = (cho__/4).toFixed(0);
	document.getElementById('cho__').innerHTML = cho__;
	document.getElementById('proportion_prot').innerHTML = (prot__/calofday*100).toFixed(1);
	document.getElementById('proportion_cho').innerHTML = (cho__/calofday*100).toFixed(1);
	
	SixType();
	drawChart(prot__, fat__, cho__, "chart2");
	
}

function SixType() {
	// 六大營養素
	quantity_Milk = parseFloat(document.getElementById('quantity_Milk').value);
	quantity_Veg = parseFloat(document.getElementById('quantity_Veg').value);
	quantity_Fruit = parseFloat(document.getElementById('quantity_Fruit').value);
	var prot = parseFloat(document.getElementById('prot').innerHTML);
	var fat = parseFloat(document.getElementById('fat').innerHTML);
	var cho = parseFloat(document.getElementById('cho').innerHTML);
	// 低脂奶類: prot 每份8g、fat每份4g、cho每份12g
	// 蔬菜: prot 每份1g、fat每份0g、cho每份5g
	// 水果: prot 每份0g、fat每份0g、cho每份15g
	// 穀類: prot 每份2g、fat每份0g、cho每份15g
	// 豆魚肉蛋: prot 每份7g、fat每份5g、cho每份0g
	// 油脂: prot 每份0g、fat每份5g、cho每份0g
	quantity_Grain = (cho - nour_cho_g - quantity_Milk * 12 - quantity_Veg * 5 - quantity_Fruit * 15) / 15;
	quantity_Grain = Adjust_quantity(quantity_Grain);

	quantity_Meat = (prot - nour_prot_g - quantity_Milk * 8 - quantity_Veg - quantity_Grain * 2) / 7;
	quantity_Meat = Adjust_quantity(quantity_Meat);

	quantity_Fat = (fat - nour_fat_g - quantity_Milk * 4 - quantity_Meat * 5) / 5;
	quantity_Fat = Adjust_quantity(quantity_Fat);

	document.getElementById('quantity_Grain').innerHTML = quantity_Grain.toString();
	document.getElementById('quantity_Meat').innerHTML = quantity_Meat.toString();
	document.getElementById('quantity_Fat').innerHTML = quantity_Fat.toString();
	document.getElementById('img_Milk_mm').innerHTML = quantity_Milk * 240+ "毫升";
	document.getElementById('img_Veg').innerHTML = quantity_Veg + "份";
	document.getElementById('img_Fruit').innerHTML = quantity_Fruit + "份";
	document.getElementById('img_Grain').innerHTML = Adjust_quantity(quantity_Grain / 4)+ "碗";
	document.getElementById('img_Meat').innerHTML = quantity_Meat + "份";
	document.getElementById('img_Fat').innerHTML = "油脂"+ Adjust_quantity((quantity_Fat - 1) / 3) + "湯匙及堅果種子類1份";

	Meal_Assign();
	Compare();
}

function Meal_Assign() {
	proportion_breakfast = parseFloat(document.getElementById('proportion_breakfast').value) / 100;
	proportion_lunch = parseFloat(document.getElementById('proportion_lunch').value) / 100;
	proportion_dinner = parseFloat(document.getElementById('proportion_dinner').value) / 100;

	// 分配奶類餐次
	var breakfast_Milk = quantity_Milk;
	var lunch_Milk = 0.0;
	var dinner_Milk = 0.0;
	// 分配蔬菜類餐次
	var breakfast_Veg = Adjust_quantity(quantity_Veg * proportion_breakfast);
	var lunch_Veg = Adjust_quantity(quantity_Veg * proportion_lunch);
	var dinner_Veg = quantity_Veg - breakfast_Veg - lunch_Veg;
	// 分配水果類餐次
	var breakfast_Fruit = Adjust_quantity(quantity_Fruit * proportion_breakfast);
	var lunch_Fruit = Adjust_quantity(quantity_Fruit * proportion_lunch);
	var dinner_Fruit = quantity_Fruit - breakfast_Fruit - lunch_Fruit;
	// 分配全穀根莖類餐次
	var breakfast_Grain = Adjust_quantity(quantity_Grain * proportion_breakfast);
	var lunch_Grain = Adjust_quantity(quantity_Grain * proportion_lunch);
	var dinner_Grain = quantity_Grain - breakfast_Grain - lunch_Grain;
	// 分配豆魚肉蛋類餐次
	var breakfast_Meat = Adjust_quantity(quantity_Meat * proportion_breakfast);
	var lunch_Meat = Adjust_quantity(quantity_Meat * proportion_lunch);
	var dinner_Meat = quantity_Meat - breakfast_Meat - lunch_Meat;
	// 分配油脂及堅果種子類餐次
	var breakfast_Fat = Adjust_quantity(quantity_Fat * proportion_breakfast);
	var lunch_Fat = Adjust_quantity(quantity_Fat * proportion_lunch);
	var dinner_Fat = quantity_Fat - breakfast_Fat - lunch_Fat;

	Write_Meal('Milk', breakfast_Milk, lunch_Milk, dinner_Milk, 0, 0, 0);
	Write_Meal('Veg', breakfast_Veg, lunch_Veg, dinner_Veg, 0, 0, 0);
	Write_Meal('Fruit', breakfast_Fruit, lunch_Fruit, dinner_Fruit, 0, 0, 0);
	Write_Meal('Grain', breakfast_Grain, lunch_Grain, dinner_Grain, 0, 0, 0);
	Write_Meal('Meat', breakfast_Meat, lunch_Meat, dinner_Meat, 0, 0, 0);
	Write_Meal('Fat', breakfast_Fat, lunch_Fat, dinner_Fat, 0, 0, 0);

}

function Dessert_quantity(quantityID, Category) {
	var quantity = parseFloat(document.getElementById(quantityID).value);
	var breakfast = parseFloat(document.getElementById("breakfast_" + Category).value);
	var lunch = parseFloat(document.getElementById("lunch_" + Category).value);
	var dinner = parseFloat(document.getElementById("dinner_" + Category).value);

	var breakfast_dessert = 
		parseFloat(document.getElementById("breakfast_dessert_" + Category).value);
	if (!isFinite(breakfast_dessert))
		breakfast_dessert = 0;
	
	var lunch_dessert = parseFloat(document.getElementById("lunch_dessert_" + Category).value);
	if (!isFinite(lunch_dessert))
		lunch_dessert = 0;
	
	var dinner_dessert = parseFloat(document.getElementById("dinner_dessert_" + Category).value);
	if (!isFinite(dinner_dessert))
		dinner_dessert = 0;

	var proportion_breakfast = parseFloat(document.getElementById('proportion_breakfast').value) / 100;
	var proportion_lunch = parseFloat(document.getElementById('proportion_lunch').value) / 100;
	var proportion_dinner = parseFloat(document.getElementById('proportion_dinner').value) / 100;

	// 取六大類建議份數
	if (Category == "Milk" || Category == "Veg" || Category == "Fruit") {
		quantity = parseFloat(document.getElementById(quantityID).value);
	} else {
		quantity = parseFloat(document.getElementById(quantityID).innerHTML);
	}
	// 總份數扣除點心份數後，依百分比分配到各餐
	if (Category == "Milk") {
		if ((breakfast_dessert + lunch_dessert + dinner_dessert) > quantity) {
			alert("已超過總份量");
			document.getElementById("breakfast_dessert_" + Category).value = "";
			document.getElementById("lunch_dessert_" + Category).value = "";
			document.getElementById("dinner_dessert_" + Category).value = "";
		} 
		else 
			quantity = quantity - (breakfast_dessert + lunch_dessert + dinner_dessert);
		
		breakfast = quantity;
		lunch = 0.0;
		dinner = 0.0;
	} 
	else if (Category != "Milk") {
		if ((breakfast_dessert + lunch_dessert + dinner_dessert) > quantity) {
			alert("已超過總份量");
			document.getElementById("breakfast_dessert_" + Category).value = "";
			document.getElementById("lunch_dessert_" + Category).value = "";
			document.getElementById("dinner_dessert_" + Category).value = "";
		} 
		else 
			quantity = quantity - (breakfast_dessert + lunch_dessert + dinner_dessert);
		
		breakfast = Adjust_quantity(quantity * proportion_breakfast);
		lunch = Adjust_quantity(quantity * proportion_lunch);
		dinner = quantity - breakfast - lunch;
	}
	
	Write_Meal(Category, breakfast, lunch, dinner);
}


function Total_intake() {	//讀取資料後，取六大類飲食攝取份量，計算攝取熱量
	prot_g = 0;
	fat_g = 0;
	cho_g = 0;
	pre_prot = 0;
	pre_fat = 0;
	pre_cho = 0;
	
	Change_Milk();
	Change_LowMilk();
	Change_HighMilk();
	Change_Veg();
	Change_Fruit();
	Change_Grain();
	Change_LowMeat();
	Change_Meat();
	Change_HighMeat();
	Change_Fat();

	Nutrients();
	Dessert_quantity('quantity_Milk', 'Milk');
	Dessert_quantity('quantity_Veg', 'Veg');
	Dessert_quantity('quantity_Fruit', 'Fruit');
	Dessert_quantity('quantity_Grain', 'Grain');
	Dessert_quantity('quantity_Meat', 'Meat');
	Dessert_quantity('quantity_Fat', 'Fat');
	
	read = false;
}

function Change_Milk() {
	if (!read) {
		prot_g -= milk * 8;
		cho_g -= milk * 12;
	}
	
	milk = 0;
	if (isFinite(parseFloat(document.getElementById('milk1').value)))
		milk = parseFloat(document.getElementById('milk1').value);
	if (isFinite(parseFloat(document.getElementById('milk2').value)))
		milk += parseFloat(document.getElementById('milk2').value);
	if (isFinite(parseFloat(document.getElementById('milk3').value)))
		milk += parseFloat(document.getElementById('milk3').value);
	if (isFinite(parseFloat(document.getElementById('milk4').value)))
		milk += parseFloat(document.getElementById('milk4').value);
	if (isFinite(parseFloat(document.getElementById('milk5').value)))
		milk += parseFloat(document.getElementById('milk5').value);
	if (isFinite(parseFloat(document.getElementById('milk6').value)))
		milk += parseFloat(document.getElementById('milk6').value);

	prot_g += milk * 8;
	cho_g += milk * 12;

	if (!read)
		Nutrients();
}

function Change_LowMilk() {
	if (!read) {
		prot_g -= low_milk * 8;
		fat_g -= low_milk * 4;
		cho_g -= low_milk * 12;
	}
	
	low_milk = 0;
	if (isFinite(parseFloat(document.getElementById('low_milk1').value)))
		low_milk = parseFloat(document.getElementById('low_milk1').value);
	if (isFinite(parseFloat(document.getElementById('low_milk2').value)))
		low_milk += parseFloat(document.getElementById('low_milk2').value);
	if (isFinite(parseFloat(document.getElementById('low_milk3').value)))
		low_milk += parseFloat(document.getElementById('low_milk3').value);
	if (isFinite(parseFloat(document.getElementById('low_milk4').value)))
		low_milk += parseFloat(document.getElementById('low_milk4').value);
	if (isFinite(parseFloat(document.getElementById('low_milk5').value)))
		low_milk += parseFloat(document.getElementById('low_milk5').value);
	if (isFinite(parseFloat(document.getElementById('low_milk6').value)))
		low_milk += parseFloat(document.getElementById('low_milk6').value);

	prot_g += low_milk * 8;
	fat_g += low_milk * 4;
	cho_g += low_milk * 12;

	if (!read)
		Nutrients();
}

function Change_HighMilk() {
	if (!read) {
		prot_g -= high_milk * 8;
		fat_g -= high_milk * 8;
		cho_g -= high_milk * 12;
	}
	
	high_milk = 0;
	if (isFinite(parseFloat(document.getElementById('high_milk1').value)))
		high_milk = parseFloat(document.getElementById('high_milk1').value);
	if (isFinite(parseFloat(document.getElementById('high_milk2').value)))
		high_milk += parseFloat(document.getElementById('high_milk2').value);
	if (isFinite(parseFloat(document.getElementById('high_milk3').value)))
		high_milk += parseFloat(document.getElementById('high_milk3').value);
	if (isFinite(parseFloat(document.getElementById('high_milk4').value)))
		high_milk += parseFloat(document.getElementById('high_milk4').value);
	if (isFinite(parseFloat(document.getElementById('high_milk5').value)))
		high_milk += parseFloat(document.getElementById('high_milk5').value);
	if (isFinite(parseFloat(document.getElementById('high_milk6').value)))
		high_milk += parseFloat(document.getElementById('high_milk6').value);

	prot_g += high_milk * 8;
	fat_g += high_milk * 8;
	cho_g += high_milk * 12;
	
	if (!read)
		Nutrients();
}

function Change_Veg() {
	if (!read) {
		prot_g -= veg;
		cho_g -= veg * 5;
	}
	
	veg = 0;
	if (isFinite(parseFloat(document.getElementById('veg1').value)))
		veg = parseFloat(document.getElementById('veg1').value);
	if (isFinite(parseFloat(document.getElementById('veg2').value)))
		veg += parseFloat(document.getElementById('veg2').value);
	if (isFinite(parseFloat(document.getElementById('veg3').value)))
		veg += parseFloat(document.getElementById('veg3').value);
	if (isFinite(parseFloat(document.getElementById('veg4').value)))
		veg += parseFloat(document.getElementById('veg4').value);
	if (isFinite(parseFloat(document.getElementById('veg5').value)))
		veg += parseFloat(document.getElementById('veg5').value);
	if (isFinite(parseFloat(document.getElementById('veg6').value)))
		veg += parseFloat(document.getElementById('veg6').value);

	prot_g += veg;
	cho_g += veg * 5;

	if (!read)
		Nutrients();
}

function Change_Fruit() {
	if (!read) 
		cho_g -= fruit * 15;
	
	fruit = 0;
	if (isFinite(parseFloat(document.getElementById('fruit1').value)))
		fruit = parseFloat(document.getElementById('fruit1').value);
	if (isFinite(parseFloat(document.getElementById('fruit2').value)))
		fruit += parseFloat(document.getElementById('fruit2').value);
	if (isFinite(parseFloat(document.getElementById('fruit3').value)))
		fruit += parseFloat(document.getElementById('fruit3').value);
	if (isFinite(parseFloat(document.getElementById('fruit4').value)))
		fruit += parseFloat(document.getElementById('fruit4').value);
	if (isFinite(parseFloat(document.getElementById('fruit5').value)))
		fruit += parseFloat(document.getElementById('fruit5').value);
	if (isFinite(parseFloat(document.getElementById('fruit6').value)))
		fruit += parseFloat(document.getElementById('fruit6').value);

	cho_g += fruit * 15;

	if (!read)
		Nutrients();
}

function Change_Grain() {
	if (!read) {
		prot_g -= grain * 2;
		cho_g -= grain * 15;
	}
	
	grain = 0;
	if (isFinite(parseFloat(document.getElementById('grain1').value)))
		grain = parseFloat(document.getElementById('grain1').value);
	if (isFinite(parseFloat(document.getElementById('grain2').value)))
		grain += parseFloat(document.getElementById('grain2').value);
	if (isFinite(parseFloat(document.getElementById('grain3').value)))
		grain += parseFloat(document.getElementById('grain3').value);
	if (isFinite(parseFloat(document.getElementById('grain4').value)))
		grain += parseFloat(document.getElementById('grain4').value);
	if (isFinite(parseFloat(document.getElementById('grain5').value)))
		grain += parseFloat(document.getElementById('grain5').value);
	if (isFinite(parseFloat(document.getElementById('grain6').value)))
		grain += parseFloat(document.getElementById('grain6').value);

	prot_g += grain * 2;
	cho_g += grain * 15;

	if (!read)
		Nutrients();
}

function Change_LowMeat() {
	if (!read) {
		prot_g -= low_meat * 7;
		fat_g -= low_meat * 3;
	}
	
	low_meat = 0;
	if (isFinite(parseFloat(document.getElementById('low_meat1').value)))
		low_meat = parseFloat(document.getElementById('low_meat1').value);
	if (isFinite(parseFloat(document.getElementById('low_meat2').value)))
		low_meat += parseFloat(document.getElementById('low_meat2').value);
	if (isFinite(parseFloat(document.getElementById('low_meat3').value)))
		low_meat += parseFloat(document.getElementById('low_meat3').value);
	if (isFinite(parseFloat(document.getElementById('low_meat4').value)))
		low_meat += parseFloat(document.getElementById('low_meat4').value);
	if (isFinite(parseFloat(document.getElementById('low_meat5').value)))
		low_meat += parseFloat(document.getElementById('low_meat5').value);
	if (isFinite(parseFloat(document.getElementById('low_meat6').value)))
		low_meat += parseFloat(document.getElementById('low_meat6').value);

	prot_g += low_meat * 7;
	fat_g += low_meat * 3;
	
	if (!read)
		Nutrients();
}

function Change_Meat() {
	if (!read) {
		prot_g -= meat * 7;
		fat_g -= meat * 5;
	}
	
	meat = 0;
	if (isFinite(parseFloat(document.getElementById('meat1').value)))
		meat = parseFloat(document.getElementById('meat1').value);
	if (isFinite(parseFloat(document.getElementById('meat2').value)))
		meat += parseFloat(document.getElementById('meat2').value);
	if (isFinite(parseFloat(document.getElementById('meat3').value)))
		meat += parseFloat(document.getElementById('meat3').value);
	if (isFinite(parseFloat(document.getElementById('meat4').value)))
		meat += parseFloat(document.getElementById('meat4').value);
	if (isFinite(parseFloat(document.getElementById('meat5').value)))
		meat += parseFloat(document.getElementById('meat5').value);
	if (isFinite(parseFloat(document.getElementById('meat6').value)))
		meat += parseFloat(document.getElementById('meat6').value);

	prot_g += meat * 7;
	fat_g += meat * 5;

	if (!read)
		Nutrients();
}

function Change_HighMeat() {
	if (!read) {
		prot_g -= high_meat * 7;
		fat_g -= high_meat * 10;
	}
	
	high_meat = 0;
	if (isFinite(parseFloat(document.getElementById('high_meat1').value)))
		high_meat += parseFloat(document.getElementById('high_meat1').value);
	if (isFinite(parseFloat(document.getElementById('high_meat2').value)))
		high_meat += parseFloat(document.getElementById('high_meat2').value);
	if (isFinite(parseFloat(document.getElementById('high_meat3').value)))
		high_meat += parseFloat(document.getElementById('high_meat3').value);
	if (isFinite(parseFloat(document.getElementById('high_meat4').value)))
		high_meat += parseFloat(document.getElementById('high_meat4').value);
	if (isFinite(parseFloat(document.getElementById('high_meat5').value)))
		high_meat += parseFloat(document.getElementById('high_meat5').value);
	if (isFinite(parseFloat(document.getElementById('high_meat6').value)))
		high_meat += parseFloat(document.getElementById('high_meat6').value);

	prot_g += high_meat * 7;
	fat_g += high_meat * 10;

	if (!read)
		Nutrients();
}

function Change_Fat() {
	if (!read) 
		fat_g -= _fat * 5;
	
	_fat = 0;
	if (isFinite(parseFloat(document.getElementById('fat1').value)))
		_fat = parseFloat(document.getElementById('fat1').value);
	if (isFinite(parseFloat(document.getElementById('fat2').value)))
		_fat += parseFloat(document.getElementById('fat2').value);
	if (isFinite(parseFloat(document.getElementById('fat3').value)))
		_fat += parseFloat(document.getElementById('fat3').value);
	if (isFinite(parseFloat(document.getElementById('fat4').value)))
		_fat += parseFloat(document.getElementById('fat4').value);
	if (isFinite(parseFloat(document.getElementById('fat5').value)))
		_fat += parseFloat(document.getElementById('fat5').value);
	if (isFinite(parseFloat(document.getElementById('fat6').value)))
		_fat += parseFloat(document.getElementById('fat6').value);

	fat_g += _fat * 5;

	if (!read)
		Nutrients();
}

function Nutrients() {
	var prot_ = parseFloat(document.getElementById('prot_').innerHTML);
	var fat_ = parseFloat(document.getElementById('fat_').innerHTML);
	var cho_ = parseFloat(document.getElementById('cho_').innerHTML);
	var calofday_intake = 0;

	prot_ = Math.round(prot_ + (prot_g - pre_prot) * 4);
	fat_ = Math.round(fat_ + (fat_g - pre_fat) * 9);
	cho_ = Math.round(cho_ + (cho_g - pre_cho) * 4);
	calofday_intake = prot_ + fat_ + cho_;
	
	document.getElementById('prot_').innerHTML = prot_;
	document.getElementById('fat_').innerHTML = fat_;
	document.getElementById('cho_').innerHTML = cho_;
	document.getElementById('prot_intake').innerHTML = (prot_/4).toFixed(0);
	document.getElementById('fat_intake').innerHTML = (fat_/9).toFixed(0);
	document.getElementById('cho_intake').innerHTML = (cho_/4).toFixed(0);
	document.getElementById('prot_rate').innerHTML = (prot_/calofday_intake*100).toFixed(1);
	document.getElementById('fat_rate').innerHTML = (fat_/calofday_intake*100).toFixed(1);
	document.getElementById('cho_rate').innerHTML = (cho_/calofday_intake*100).toFixed(1);
	document.getElementById('calofday_intake').innerHTML = calofday_intake;
	document.getElementById('average1').innerHTML = "(" + (calofday_intake/weight1).toFixed(1) + "大卡/公斤)";
	pre_prot = prot_g;
	pre_fat = fat_g;
	pre_cho = cho_g;

	drawChart(prot_, fat_, cho_, "chart1"); // 繪製圓餅圖
	Compare(); // 飲食建議與攝取比較
}

function Compare() {
	var tempStr = "";
	var milk_compare = quantity_Milk - (milk + low_milk + high_milk);

	if (high_milk > 0) {
		tempStr = "*";
		document.getElementById('warning1').innerHTML = "*奶類建議更改低脂或脫脂";
	} 
	else // high_milk = 0	
		document.getElementById('warning1').innerHTML = "";

	if (milk_compare > 0) // 不足
	{
		document.getElementById('milk_compare').style.color = '#009900';
		document.getElementById('milk_compare').innerHTML = "不足" + milk_compare + "份"+ tempStr;
	} 
	else if (milk_compare == 0) // 符合標準
	{
		document.getElementById('milk_compare').style.color = '#000000';
		document.getElementById('milk_compare').innerHTML = "符合標準" + tempStr;
	} 
	else // if ( milk_compare < 0 ) //超過
	{
		document.getElementById('milk_compare').style.color = '#FF0000';
		document.getElementById('milk_compare').innerHTML = "超過" + (0 - milk_compare) + "份" + tempStr;
	}

	var veg_compare = quantity_Veg - veg;
	if (veg_compare > 0) // 不足
	{
		document.getElementById('veg_compare').style.color = '#009900';
		document.getElementById('veg_compare').innerHTML = "不足" + veg_compare + "份";
	} 
	else if (veg_compare == 0) // 符合標準
	{
		document.getElementById('veg_compare').style.color = '#000000';
		document.getElementById('veg_compare').innerHTML = "符合標準";
	} 
	else // if ( veg_compare < 0 ) //超過
	{
		document.getElementById('veg_compare').style.color = '#FF0000';
		document.getElementById('veg_compare').innerHTML = "超過" + (0 - veg_compare) + "份";
	}

	var fruit_compare = quantity_Fruit - fruit;
	if (fruit_compare > 0) // 不足
	{
		document.getElementById('fruit_compare').style.color = '#009900';
		document.getElementById('fruit_compare').innerHTML = "不足" + fruit_compare + "份";
	} 
	else if (fruit_compare == 0) // 符合標準
	{
		document.getElementById('fruit_compare').style.color = '#000000';
		document.getElementById('fruit_compare').innerHTML = "符合標準";
	} 
	else // if ( fruit_compare < 0 ) //超過
	{
		document.getElementById('fruit_compare').style.color = '#FF0000';
		document.getElementById('fruit_compare').innerHTML = "超過" + (0 - fruit_compare) + "份";
	}

	var grain_compare = quantity_Grain - grain;
	if (grain_compare > 0) // 不足
	{
		document.getElementById('grain_compare').style.color = '#009900';
		document.getElementById('grain_compare').innerHTML = "不足" + grain_compare + "份";
	} 
	else if (grain_compare == 0) // 符合標準
	{
		document.getElementById('grain_compare').style.color = '#000000';
		document.getElementById('grain_compare').innerHTML = "符合標準";
	} 
	else // if ( grain_compare < 0 ) //超過
	{
		document.getElementById('grain_compare').style.color = '#FF0000';
		document.getElementById('grain_compare').innerHTML = "超過" + (0 - grain_compare) + "份";
	}

	var meat_compare = quantity_Meat - low_meat - meat - high_meat;
	if (high_meat > 0) {
		tempStr = "*";
		document.getElementById('warning2').innerHTML = "*肉類建議更改中脂或低脂";
	} 
	else // high_meat = 0
	{
		tempStr = "";
		document.getElementById('warning2').innerHTML = "";
	}

	if (meat_compare > 0) // 不足
	{
		document.getElementById('meat_compare').style.color = '#009900';
		document.getElementById('meat_compare').innerHTML = "不足" + meat_compare + "份" + tempStr;
	} 
	else if (meat_compare == 0) // 符合標準
	{
		document.getElementById('meat_compare').style.color = '#000000';
		document.getElementById('meat_compare').innerHTML = "符合標準" + tempStr;
	} 
	else // if ( meat_compare < 0 ) //超過
	{
		document.getElementById('meat_compare').style.color = '#FF0000';
		document.getElementById('meat_compare').innerHTML = "超過" + (0 - meat_compare) + "份" + tempStr;
	}

	var fat_compare = quantity_Fat - _fat;
	if (fat_compare > 0) // 不足
	{
		document.getElementById('fat_compare').style.color = '#009900';
		document.getElementById('fat_compare').innerHTML = "不足" + fat_compare + "份";
	} 
	else if (fat_compare == 0) // 符合標準
	{
		document.getElementById('fat_compare').style.color = '#000000';
		document.getElementById('fat_compare').innerHTML = "符合標準";
	} 
	else // if ( fat_compare < 0 ) //超過
	{
		document.getElementById('fat_compare').style.color = '#FF0000';
		document.getElementById('fat_compare').innerHTML = "超過" + (0 - fat_compare) + "份";
	}

	drawChart3();
}

function Adjust_quantity(quantity) {	//進位方法
	var decimal = quantity - parseInt(quantity);
	if (quantity < 0)
		quantity = 0;
	else if (decimal <= 0.2)	//  0～0.2直接捨去
		quantity = parseInt(quantity);
	else if (decimal >= 0.8)	//  0.8～1直接進位
		quantity = parseInt(quantity) + 1;
	else						//  0.2～0.8設為0.5
		quantity = parseInt(quantity) + 0.5;
	
	return quantity;
}

function meal_proportion(pro_input, pro_output) {
	var input_proportion = document.getElementById("proportion_" + pro_input).value;
	var output_proportion;
	
	if (pro_input == "breakfast") {
		output_proportion = (100 - input_proportion) / 2;
		document.getElementById('proportion_lunch').value = output_proportion;
		document.getElementById('proportion_dinner').value = output_proportion;
	} 
	else {
		var proportion_breakfast = document
				.getElementById('proportion_breakfast').value;
		output_proportion = 100 - input_proportion - proportion_breakfast;
		document.getElementById("proportion_" + pro_output).value = output_proportion;
	}
	
	SixType();
}

function Write_Meal(Category, breakfast, lunch, dinner) {
	document.getElementById("breakfast_" + Category).innerHTML = breakfast;
	document.getElementById("lunch_" + Category).innerHTML = lunch;
	document.getElementById("dinner_" + Category).innerHTML = dinner;
}

function showtabel(tableid) {
	if (document.getElementById(tableid).className == "off")
		document.getElementById(tableid).className = "on";
	else
		document.getElementById(tableid).className = "off";
}

function check(onclick) {		//六大類禁忌、可用食物連結
	var id = document.getElementById("id").value;
	
	if(typeof(id) === "undefined" || id == ""){
		alert("請填寫身分證字號");
		return;
	}
	if (checkID(id)){
		var foodwork_obj = document.getElementsByName("foodwork");
		var foodwork_len = foodwork_obj.length;

		for (var i = 0; i < foodwork_len; i++) 
			foodwork_obj[i].checked = false;
		
		var str = onclick;
		document.getElementById(str).checked = true;
		document.menuform.submit();
	}else{
		document.getElementById("id").value = "";
		return;
	}
}

function drawChart(data1, data2, data3, chart1) {  //圓餅圖繪製
	
	var data = google.visualization.arrayToDataTable([
		[' ', ' '], ['蛋白質', data1], ['脂肪', data2], ['醣類', data3]]);

	var options = {
            'fontSize' : 23, 'fontName' : '標楷體',
            chartArea : {left : 25, top : 20, width : "80%", height : "80%"}
		};

	var chart = new google.visualization.PieChart(document.getElementById(chart1));
	chart.draw(data, options);
}

function drawChart3() {
	var data = google.visualization.arrayToDataTable([
			[ '六類大類食物', '建議', '實際' ],
			[ '奶類', quantity_Milk, (milk + low_milk + high_milk) ],
			[ '蔬菜類', quantity_Veg, veg], 
			[ '水果類', quantity_Fruit, fruit ],
			[ '全穀類', quantity_Grain, grain ],
			[ '豆魚肉蛋', quantity_Meat, (low_meat + meat + high_meat) ],
			[ '油脂類', quantity_Fat, _fat ] ]);

	var options = {
		'fontSize' : 20, 'fontName' : '標楷體',
		'height' : 250, 'width' : 800, colors:['#CCCCCC', 'blue'],
		chartArea : { left : 30, width : "80%", height : "75%"}
	};

	var chart = new google.visualization.ColumnChart(document.getElementById('chart3'));
	chart.draw(data, options);
}

function printout() { // 列印
	if (!window.print) {
		alert("列印功能暫時停用，請按 Ctrl-P 來列印");
		return;
	}
	
	document.body.style.backgroundImage = "url('none')";
	window.print();
	document.body.style.backgroundImage = "url('img/grass.jpg')";

}
function MM_openBrWindow(theURL, winName, features) {
	window.open(theURL, winName, features);
}

function printpage() { // 列印

	if (!window.print) {
		alert("列印功能暫時停用，請按 Ctrl-P 來列印");
		return;
	}
	window.print();
}